const app = getApp();
const path = app.globalData.path;
//Page Object
Page({
    data: {
        issueId: null,
        todolistData: [],
        issueInfo: '',
        path: [],
        imgs: [],
        upload_picture_list: [],
        buttonStatus: '点击显示',
        buttonStatusCls: 'hidden',
    },

    toggleBtn: function (e) {
        var that = this;
        if (this.data.buttonStatusCls == 'hidden') {
            that.setData({
                buttonStatus: '点击隐藏',
                buttonStatusCls: 'show'
            })
        } else if (this.data.buttonStatusCls == 'show') {
            that.setData({
                buttonStatus: '点击显示',
                buttonStatusCls: 'hidden'
            })
        }
    },

  // 预览图片
  previewImg(e) {
    console.log(e);
    //获取当前图片的下标
    let index = e.currentTarget.dataset.index;
    console.log(index);
    console.log(this.data.picList);
    //所有图片
    let imgs = this.data.picList;
    var imgPaths = [];
    for (var i = 0; i < imgs.length; i++) {
      imgPaths.push(imgs[i].path);
      console.log(imgPaths);
    }
    wx.previewImage({
      //当前显示图片
      current: imgs[index].path,
      //所有图片
      urls: imgPaths
    })
  },
    //options(Object)
    onLoad: function (options) {
        var that = this;
        that.setData({
            lifecycleStatus: options.lifecycleStatus,
            processStatus: options.processStatus,
            issueId: options.issueId,
            programVehicleId: options.programVehicleId
        });

        let loginFlag = wx.getStorageSync('token');
        if (loginFlag) {
            // 查询问题信息
            wx.request({
                url: path + 'wx-issue/q-iissue!getQIIssueInfo.action',
                header: {
                    'token': loginFlag
                },
                data: {
                    issueId: options.issueId
                },
                dataType: "JSON",
                success: function (res) {
                    var result = JSON.parse(res.data);
                    var issueInfo = result.data;


                    if (issueInfo.lifecycleStatus == 'DRAFT') {
                        that.setData({status: '草稿'})
                    } else if (issueInfo.lifecycleStatus == 'OPEN') {
                        that.setData({status: '待处理'})
                    } else if (issueInfo.lifecycleStatus == 'PENDING') {
                        that.setData({status: '进行中'})
                    } else {
                        that.setData({status: '已关闭'})
                    }
                    that.setData({
                        issueInfo: result.data
                    })
                    that.getPartTypeName(result, loginFlag, that);
                    that.getIssueTitle(result, loginFlag, that);
                    that.getIssueTypeTitle(result, loginFlag, that);
                },
                fail: function () {
                    console.log("fail");
                }

            })
            // 查询责任领域  传入车型
            wx.request({
                url: path + 'wx-pm/project-open-issue!getResponsibleObsList.action',
                header: {
                    'token': loginFlag
                },
                data: {
                    programVehicleId: options.programVehicleId
                },
                dataType: "JSON",
                success: function (res) {
                    // console.log(res);
                    var result = JSON.parse(res.data);
                    var functionId = that.data.issueInfo.responsibleFunction;
                    var resoResult = result.data;
                    for (var i = 0; i < resoResult.length; i++) {
                        if (functionId == resoResult[i].id) {

                            that.setData({
                                resoName: resoResult[i].name
                            })
                            console.log(resoName);
                            return;
                        }
                    }
                },
                fail: function () {
                    console.log("fail");
                }
            })

            wx.showLoading({
                title: '图片加载中...',
            })
            // 根据sourceId source 查询图片列表

            
            wx.request({
                url: path + 'wx-com/image!selectCmImageList.action',
                header: {
                    'token': loginFlag
                },
                data: {
                    sourceId: options.issueId,
                    source: 'QIM_ISSUE_IMAGE'
                },
                dataType: "JSON",

                success: function (res) {
                    let result = JSON.parse(res.data);
                    let picIdList = result.data;
                    let picList = [];

                    for (let i = 0; i < picIdList.length; i++) {
                        let img = {};
                        img.imageId = picIdList[i].id;
                        img.path = path + 'wx-com/image!downloadCmImage.action?id=' + picIdList[i].id;
                        picList.push(img);
                    }

                    // console.log(result.data);
                    that.setData({
                        picList: picList,

                    })
                    wx.hideLoading();
                },
                fail: function () {
                    console.log("fail");
                }
            })

        }


    },

    getPartTypeName: function (result, loginFlag, that) {

        // 根据问题信息已返回的结果productTypes作为参数 再发送一个request .
        wx.request({
            url: path + 'wx-issue/q-iissue!getPmPartTypeList.action',

            header: {
                'token': loginFlag
            },
            dataType: "JSON",
            data: {
                productTypes: result.data.productTypes
            },
            success: function (r) {
                // console.log(r.data);
                var pmPartTypes = JSON.parse(r.data);

                // 从返回的数据中找到 问题信息 中对应的零件类别
                // that.setData({
                //   productTypeList: pmPartTypes.data
                // })

                var partArray = pmPartTypes.data;
                var issuePartTypeId = result.data.partTypeId;  // 获取问题返回结果中的 partTypeId

                for (var i = 0; i < partArray.length; i++) {
                    if (issuePartTypeId == partArray[i].id) {
                        that.setData({
                            partTypeName: partArray[i].name
                        })
                        return;
                    }
                }
            },
            fail: function (res) {
                console.log(res);
            }
        })
    },

    getIssueTitle: function (result, loginFlag, that) {
        // 根据问题信息已返回的结果
        wx.request({
            url: path + 'wx-issue/q-iissue!getIssueSourceList.action',
            header: {
                'token': loginFlag
            },
            dataType: "JSON",
            success: function (response) {

                var sourceList = JSON.parse(response.data);
                var sourceArray = sourceList.data;
                var issueSourceId = result.data.issueSourceId;  // 获取问题信息返回结果中的 issueSourceId

                for (var i = 0; i < sourceArray.length; i++) {
                    if (issueSourceId == sourceArray[i].id) {
                        that.setData({
                            issueSourceTitle: sourceArray[i].title
                        })
                        return;
                    }
                }
            },
            fail: function (res) {
                console.log(res);
            }
        })
    },

    getIssueTypeTitle: function (result, loginFlag, that) {
        var that = this;
        // var path = getApp().path;
        wx.request({
            url: path + 'wx-issue/q-iissue!getIssueTypeList.action',
            //发送的数据
            header: {
                'token': loginFlag
            },
            dataType: "JSON",
            //成功回调
            success: function (res) {

                var issueType = JSON.parse(res.data);
                var issueTypeArray = issueType.data;
                var issueTypeId = result.data.issueTypeId;

                for (var i = 0; i < issueTypeArray.length; i++) {
                    if (issueTypeId == issueTypeArray[i].id) {
                        that.setData({
                            issueTypeTitle: issueTypeArray[i].title
                        })
                        return;
                    }
                }
            },
            fail: function (res) {
                console.log(res);
            }
        })
    },

    onReady: function () {

    },
    onShow: function () {

    },
    onHide: function () {

    },
    onUnload: function () {

    },
    onPullDownRefresh: function () {

    },
    onReachBottom: function () {

    },
    onShareAppMessage: function () {

    },
    onPageScroll: function () {

    },
    //item(index,pagePath,text)
    onTabItemTap: function (item) {

    }
});